import 'dart:io';

import 'package:chatgreat/repo/api_server.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../page/login/model/tourist_login_model.dart';


class AppUtil {
  /// 单例
  static final AppUtil _instance = AppUtil._internal();
  AppUtil._internal();
  factory AppUtil() {
    return _instance;
  }
  GlobalKey<NavigatorState>? rootNavigatorKey;

  TouristLoginModel? touristLoginModel;

  final String FIRST_TIME_OPEN_KEY = 'FIRST_TIME_OPEN_KEY';

  final String IS_LOGIN_KEY = 'IS_LOGIN_KEY';

  // 是否评分过
  final String HOME_IN_APP_REVIEW_ALERT = 'HOME_IN_APP_REVIEW_ALERT';

  Future<bool> updateUserInfo() async {
    touristLoginModel = await APIServer().getUserInfo();
    return true;
  }

  Future<bool> isFirstTime() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    bool isFirstTime = prefs.getBool(FIRST_TIME_OPEN_KEY) ?? true;

    if (isFirstTime) {
      // 是首次安装应用
      await prefs.setBool(FIRST_TIME_OPEN_KEY, false);
      return true;
    } else {
      // 不是首次安装应用
      return false;
    }
  }

  void setLogin(bool login) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool(IS_LOGIN_KEY, login);
  }

  Future<bool> isLogin() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    bool isLogin = prefs.getBool(IS_LOGIN_KEY) ?? false;
    return isLogin;
  }


  Future<bool> isAppReview() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    bool isReview = prefs.getBool(HOME_IN_APP_REVIEW_ALERT) ?? false;
    return isReview;
  }

  void setAppReview(bool review) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool(HOME_IN_APP_REVIEW_ALERT, review);
  }

}